home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / rail.zip / GRAM.Y < prev    next >
Text File  |  1992-01-15  |  3KB  |  225 lines

  1. /* gram.y - yacc grammar for rail program */
  2.  
  3. %{
  4.  
  5. #include <stdio.h>
  6.  
  7. #include "rail.h"
  8.  
  9. char optchar;
  10.  
  11. %}
  12.  
  13. /* identifier */
  14.  
  15. %token <id> IDENTIFIER
  16.  
  17. /* number */
  18.  
  19. %token <num> NUMBER
  20.  
  21. /* [annotation] */
  22.  
  23. %token <text> ANNOT
  24.  
  25. /* \rail@i \rail@p \rail@t \\ */
  26.  
  27. %token RAILI RAILP RAILT RAILCR
  28.  
  29. /* TeX control sequence */
  30.  
  31. %token CS
  32.  
  33. /* 'c' "string" */
  34.  
  35. %token <text> STRING
  36.  
  37. %type <rule> rule rules
  38.  
  39. %type <body> body body0 body1 body2e body2 body3 body4e body4 empty
  40.  
  41. %type <text> annot
  42.  
  43. %start rails
  44.  
  45. %%
  46.  
  47. rails    : rails rail
  48.     | rail
  49.     ;
  50.  
  51. rail    : RAILI raili
  52.     | RAILP railp
  53.     | RAILT railt
  54.     | error
  55.     ;
  56.  
  57. railp    :
  58.       '{'
  59.         {
  60.             fprintf(outf,"\\rail@p {");
  61.             copy=1;
  62.             optchar = '-'; }
  63.       options '}'
  64.         {
  65.             fprintf(outf,"\n");
  66.             copy=0;
  67.         }
  68.     ;
  69.  
  70. options    : /*empty*/
  71.     | options '-'
  72.         { optchar = '-'; }
  73.     | options '+'
  74.         { optchar = '+'; }
  75.     | options IDENTIFIER
  76.         {
  77.             if(setopt(optchar,$2->name)==0)
  78.                 error("unknown option",(char *)NULL);
  79.  
  80.             if($2->kind==UNKNOWN)
  81.                 delete($2);
  82.         }
  83.     ;
  84.  
  85. railt    : '{' IDENTIFIER '}'
  86.         {
  87.             if($2->kind==UNKNOWN || $2->kind==TOKEN)
  88.                 $2->kind=TERM;
  89.             else
  90.                 redef($2);
  91.  
  92.             fprintf(outf,"\\rail@t {%s}\n",$2->name);
  93.         }
  94.     ;
  95.  
  96. raili    : '{' NUMBER '}'
  97.         {
  98.             fprintf(outf,"\\rail@i {%d}",$2);
  99.             copy=1;
  100.         }
  101.       '{' rules '}'
  102.         {    copy=0;
  103.             fprintf(outf,"\n");
  104.             fprintf(outf,"\\rail@o {%d}{\n",$2);
  105.             outrule($6);    /* embedded action is $4 */
  106.             freerule($6);
  107.             fprintf(outf,"}\n");
  108.         }
  109.     ;
  110.  
  111. rules    : rules ';' rule
  112.         { $$=addrule($1,$3); }
  113.     | rules ';'
  114.         { $$=$1; }
  115.     | rule
  116.     | error
  117.         { $$=NULL; }
  118.     ;
  119.  
  120. rule    : IDENTIFIER ':'
  121.         { errorid=$1; }
  122.           body
  123.         {
  124.             if($1->kind==UNKNOWN || $1->kind==TOKEN)
  125.                 $1->kind=NTERM;
  126.             else
  127.                 redef($1);
  128.  
  129.              $$=newrule($1,$4);    /* embedded action is $3 */
  130.  
  131.             errorid=NULL;
  132.         }
  133.     | body
  134.         {
  135.             anonymous++;
  136.  
  137.             $$=newrule((IDTYPE *)NULL,$1);
  138.         }
  139.     ;
  140.  
  141. body    : body0
  142.         { $$=$1; $$->done=1; }
  143.     ;
  144.  
  145. body0    : body0 '|' ANNOT body1
  146.         {
  147.             $$=newbody(ANNOTE,NULLBODY,NULLBODY);
  148.             $$->text=$3;
  149.             $$=addbody(CAT,$$,$4);
  150.             $$=addbody(BAR,$1,$$);
  151.         }
  152.     | body0 '|' body1
  153.         { $$=addbody(BAR,$1,$3); }
  154.     | ANNOT body1
  155.         {
  156.             $$=newbody(ANNOTE,NULLBODY,NULLBODY);
  157.             $$->text=$1;
  158.             $$=addbody(CAT,$$,$2);
  159.         }
  160.     | body1
  161.     ;
  162.  
  163. body1    : body2 '*' body4e
  164.         {
  165.             if(altstar && isemptybody($3)) {
  166.                 $$=newbody(EMPTY,NULLBODY,NULLBODY);
  167.                 $$=addbody(PLUS,$$,revbody($1));
  168.             } else {
  169.                 $$=newbody(EMPTY,NULLBODY,NULLBODY);
  170.                 $$=addbody(BAR,$$,addbody(PLUS,$1,revbody($3)));
  171.             }
  172.         }
  173.     | body2 '+' body4e
  174.         { $$=newbody(PLUS,$1,revbody($3)); }
  175.     | body2e
  176.     ;
  177.  
  178. body2e    : body2 | empty ;
  179.  
  180. body2    : body2 body3
  181.         { $$=addbody(CAT,$1,$2); }
  182.     | body3
  183.     ;
  184.  
  185. body3    : body4 '?'
  186.         { $$=addbody(BAR,newbody(EMPTY,NULLBODY,NULLBODY),$1); }
  187.     | body4
  188.     ;
  189.  
  190. body4e    : body4 | empty ;
  191.  
  192. body4    : '(' body0 ')'
  193.         { $$=$2; $$->done=1; }
  194.     | STRING annot
  195.         {
  196.             $$=newbody(STRNG,NULLBODY,NULLBODY);
  197.             $$->annot=$2;
  198.             $$->text=$1;
  199.         }
  200.     | IDENTIFIER annot
  201.         {
  202.             if($1->kind==UNKNOWN)
  203.                 $1->kind=TOKEN;
  204.  
  205.             $$=newbody(IDENT,NULLBODY,NULLBODY);
  206.             $$->annot=$2;
  207.             $$->id=$1;
  208.         }
  209.     | RAILCR
  210.         { $$=newbody(CR,NULLBODY,NULLBODY); }
  211.     ;
  212.  
  213. empty    : /*empty*/
  214.         { $$=newbody(EMPTY,NULLBODY,NULLBODY); }
  215.     ;
  216.  
  217. annot    : ANNOT
  218.         { $$=$1; }
  219.     | /*empty*/
  220.         { $$=NULL; }
  221.     ;
  222.  
  223. %%
  224.  
  225.